package com.css.common.core.session;


import cn.hutool.json.JSONUtil;
import com.css.common.core.utils.BaseUtil;
import com.css.common.core.utils.GyUtils;
import com.css.common.core.utils.RedisUtils;
import com.css.common.core.utils.ServletUtils;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.http.Cookie;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

import static com.css.common.core.constant.SsoConstants.COOKIE_TOKEN_KEY;
@Slf4j
public class SessionManager {

    private static final ThreadLocal<Session> sessionManger = new ThreadLocal<Session>();


    public static Session get() {
        Session session = sessionManger.get();
        if (Objects.isNull(session)) {
            session = new Session();
            onSessionData(session);
            sessionManger.set(session);
        }
        return session;
    }


    public static Session getClear() {
        return get().clear();
    }


    public static Session clear(Session session) {
        return session.clear();
    }

    private static void onSessionData(Session session) {
        try {
            String token = "";
            Cookie[] cookies = ServletUtils.getRequest().getCookies();
            if (GyUtils.isNull(cookies)) {
                token = ServletUtils.getRequest().getHeader(COOKIE_TOKEN_KEY);
            } else {
                List<Cookie> collect = Arrays.stream(cookies).filter(v -> v.getName().equals(COOKIE_TOKEN_KEY)).collect(Collectors.toList());
                if (!GyUtils.isNull(collect)) {
                    token = collect.get(0).getValue();
                }
            }
            if (GyUtils.isNotNull(token)) {
                Map yhxx = JSONUtil.toBean(RedisUtils.getHash(token, "yhxx"), Map.class);
                log.info("onSessionData通过token查询数据：{}", JSONUtil.toJsonStr(yhxx) + "；;token:" + token);
                if (GyUtils.isNotNull(yhxx)) {
                    log.info("session添加值");
                    //以下是zfjkp的session字段
                    session.setGlzhbz("31".equals(yhxx.get("yhdlsfdm")) ? "Y" : "N");//管理员账号标志：Y是管理
                    session.setYhdlzh(BaseUtil.object2Str(yhxx.get("yhdlzh")));//用户登录账号
                    session.setQysh(BaseUtil.object2Str(yhxx.get("qysh")));//企业税号
                    session.setQymc(BaseUtil.object2Str(yhxx.get("qymc")));//企业税号
                    session.setYhdlsfdm(BaseUtil.object2Str(yhxx.get("yhdlsfdm")));//用户登录身份代码
                    session.setQyid(BaseUtil.object2Str(yhxx.get("qyid")));//企业id
                    session.setQyrzlbDm(BaseUtil.object2Str(yhxx.get("qyrzlbDm")));//qyrzlbDm
                    session.setZrrid(BaseUtil.object2Str(yhxx.get("zrrid")));//自然人id
                    session.setZrrxm(BaseUtil.object2Str(yhxx.get("xm")));//自然人姓名
                    session.setZrrzjhm(BaseUtil.object2Str(yhxx.get("sfzjhm")));//自然人证件号码
                    session.setZrrsjhm(BaseUtil.object2Str(yhxx.get("sjh")));//自然人手机号码
                    //以下是fxkp的session字段
                    session.setZrruuid(BaseUtil.object2Str(yhxx.get("zrruuid")));
                    session.setUserType(BaseUtil.object2Str(yhxx.get("userType")));
                    session.setYhm(BaseUtil.object2Str(yhxx.get("yhm")));//用户名
                   /* session.setGlzhbz(BaseUtil.object2Str(yhxx.get("glzhbz")));//管理账户标志，Y为银联等管理平台提供账号*/
                    session.setXm(BaseUtil.object2Str(yhxx.get("xm")));//姓名
                    session.setSfzjhm(BaseUtil.object2Str(yhxx.get("sfzjhm")));//身份证件号码
                    session.setSjh(BaseUtil.object2Str(yhxx.get("sjh")));//手机号
                    session.setDz(BaseUtil.object2Str(yhxx.get("dz")));//地址
                    session.setQyuuid(BaseUtil.object2Str(yhxx.get("qyuuid")));//企业uuid
                   /* session.setQymc(BaseUtil.object2Str(yhxx.get("qymc")));//企业名称*/
                    session.setTyshxydm(BaseUtil.object2Str(yhxx.get("tyshxydm")));//统一社会信用代码
                    session.setSqzt(BaseUtil.object2Str(yhxx.get("sqzt")));//申请状态
                    session.setSfgly(BaseUtil.object2Str(yhxx.get("sfgly")));//是否管理员企业
                    session.setZhlb(BaseUtil.object2Str(yhxx.get("zhlb")));//账户类别
                    session.setMap(yhxx);
                    try {
                        session.setXtqdDm(!BaseUtil.isNull(yhxx.get("xtqdDm"))?Integer.valueOf(yhxx.get("xtqdDm")+""):null);//渠道代码
                    }catch (Exception e){
                        log.info("获取xtqdDm异常："+e);
                    }
                    session.setMap(yhxx);
                    log.info("session值zrrid:{},qysh:{}",session.getZrrid()+"",session.getQysh()+"");
                }else{
                    log.info("session查询token中信息为空");
                }
            }
        } catch (Exception e) {
            log.info("sessionException -add", e);
        }
    }
}
